/**
 * Octopus, an XMMS2 client.
 * Copyright (C) 2014 Octopus Team
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

namespace OctopusUi {
	public class UiBottomPanel : Mx.Frame {
		private const int ICONSIZE = 20;
		private Mx.Label bottomPanelLabel;
		private Mx.Button bottomPanelRefresh;
		private Mx.Button bottomPanelFullscreen;
		private Mx.Button bottomPanelImport; 
		private Mx.Button bottomPanelProperties; 
		private Mx.Button bottomPanelQuit;

		public signal void refresh_album_list ();
		public signal void toggle_fullscreen ();
		public signal void layout (LayoutEnum layout);
		public signal void css (string css_file);
		public signal void quit ();

		public UiBottomPanel () {
			var bottomPanelStack = new Mx.Stack ();
			bottomPanelStack.name = "bottomPanel-background";
			this.add_child (bottomPanelStack);

			var bottomPanelHBox = new Mx.BoxLayout ();
			bottomPanelHBox.style_class = "bottomPanel-hbox";
			bottomPanelHBox.orientation = Mx.Orientation.HORIZONTAL;
			bottomPanelStack.add_child (bottomPanelHBox);

			var bottomPanelMenu = new Mx.Button ();
			bottomPanelMenu.style_class = "bottomPanel-menu";
			bottomPanelMenu.width = ICONSIZE;
			bottomPanelMenu.height = ICONSIZE;
			bottomPanelHBox.add_child (bottomPanelMenu);
			bottomPanelMenu.clicked.connect (() => {
				this.bottomPanelRefresh.visible = !this.bottomPanelRefresh.visible;
				this.bottomPanelFullscreen.visible = !this.bottomPanelFullscreen.visible;
				this.bottomPanelImport.visible = !this.bottomPanelImport.visible;
				this.bottomPanelProperties.visible = !this.bottomPanelProperties.visible;
				this.bottomPanelQuit.visible = !this.bottomPanelQuit.visible;
			});
			bottomPanelMenu.enter_event.connect (() => {
				setText ("menu");
				return false; //propagate
			});
			bottomPanelMenu.leave_event.connect (() => {
				setText ("");
				return false; //propagate
			});
			this.bottomPanelRefresh = new Mx.Button ();
			this.bottomPanelRefresh.style_class = "bottomPanel-refresh";
			this.bottomPanelRefresh.width = ICONSIZE;
			this.bottomPanelRefresh.height = ICONSIZE;
			this.bottomPanelRefresh.visible = false;
			bottomPanelHBox.add_child (this.bottomPanelRefresh);
			this.bottomPanelRefresh.clicked.connect (() => {
				refresh_album_list ();
			});
			this.bottomPanelRefresh.enter_event.connect (() => {
				setText ("refresh albumlist");
				return false; //propagate
			});
			this.bottomPanelRefresh.leave_event.connect (() => {
				setText ("");
				return false; //propagate
			});
			this.bottomPanelFullscreen = new Mx.Button ();
			this.bottomPanelFullscreen.style_class = "bottomPanel-fullscreen";
			this.bottomPanelFullscreen.width = ICONSIZE;
			this.bottomPanelFullscreen.height = ICONSIZE;
			this.bottomPanelFullscreen.visible = false;
			bottomPanelHBox.add_child (this.bottomPanelFullscreen);
			this.bottomPanelFullscreen.clicked.connect (() => {
				toggle_fullscreen ();
			});
			this.bottomPanelFullscreen.enter_event.connect (() => {
				setText ("toggle fullscreen");
				return false; //propagate
			});
			this.bottomPanelFullscreen.leave_event.connect (() => {
				setText ("");
				return false; //propagate
			});
			this.bottomPanelImport = new Mx.Button ();
			this.bottomPanelImport.style_class = "bottomPanel-import";
			this.bottomPanelImport.width = ICONSIZE;
			this.bottomPanelImport.height = ICONSIZE;
			this.bottomPanelImport.visible = false;
			bottomPanelHBox.add_child (this.bottomPanelImport);
			this.bottomPanelImport.clicked.connect (() => {
				import_dialog ();
			});
			this.bottomPanelImport.enter_event.connect (() => {
				setText ("import folder");
				return false; //propagate
			});
			this.bottomPanelImport.leave_event.connect (() => {
				setText ("");
				return false; //propagate
			});
			this.bottomPanelProperties = new Mx.Button ();
			this.bottomPanelProperties.style_class = "bottomPanel-properties";
			this.bottomPanelProperties.width = ICONSIZE;
			this.bottomPanelProperties.height = ICONSIZE;
			this.bottomPanelProperties.visible = false;
			bottomPanelHBox.add_child (this.bottomPanelProperties);

			this.bottomPanelProperties.clicked.connect (() => {
				var properties_dialog = new UiPropertiesDialog ();
				properties_dialog.layout.connect ((layout_enum) => {
					layout (layout_enum);
				});
				properties_dialog.css.connect ((css_file) => {
					css (css_file);
				});
				properties_dialog.response.connect (() => {
					
					//properties_dialog.destroy ();
					
				});
				properties_dialog.show_all ();
			});
			this.bottomPanelProperties.enter_event.connect (() => {
				setText ("properties");
				return false; //propagate
			});
			this.bottomPanelProperties.leave_event.connect (() => {
				setText ("");
				return false; //propagate
			});
			this.bottomPanelQuit = new Mx.Button ();
			this.bottomPanelQuit.style_class = "bottomPanel-quit";
			this.bottomPanelQuit.width = ICONSIZE;
			this.bottomPanelQuit.height = ICONSIZE;
			this.bottomPanelQuit.visible = false;
			bottomPanelHBox.add_child (this.bottomPanelQuit);
			this.bottomPanelQuit.clicked.connect (() => {
				quit ();
			});
			this.bottomPanelQuit.enter_event.connect (() => {
				setText ("quit");
				return false; //propagate
			});
			this.bottomPanelQuit.leave_event.connect (() => {
				setText ("");
				return false; //propagate
			});
			this.bottomPanelLabel = new Mx.Label ();
			this.bottomPanelLabel.style_class = "bottomPanel-label";
			bottomPanelHBox.add_child (this.bottomPanelLabel);

			var bottomPanelOverlay = new Mx.Frame ();
			bottomPanelOverlay.name = "bottomPanel-overlay";
			bottomPanelStack.add_child (bottomPanelOverlay);
		}

		private void import_dialog () {
			var file_chooser = new Gtk.FileChooserDialog ("Import Folder", null,
			                                              Gtk.FileChooserAction.SELECT_FOLDER,
			                                              Gtk.Stock.CANCEL, Gtk.ResponseType.CANCEL,
			                                              Gtk.Stock.OK, Gtk.ResponseType.OK);
			file_chooser.select_multiple = true;
			file_chooser.response.connect ((response) => {
				if (response == Gtk.ResponseType.OK) {
					GLib.SList<string> filenames;
					string path;
					filenames = file_chooser.get_filenames();
					foreach(string filename in filenames) {
						path = "file://" + filename;
						GLib.debug ("fileDialog: %s", path);
						Octopus.Global.get ().get_xmms_client ().get_xmms ().medialib_import_path (path);
					}
				}
				file_chooser.destroy ();
			});
			file_chooser.show ();
			//we use code above, because code below freezes application
			// and the dialog never shows up.
			//if (file_chooser.run () == Gtk.ResponseType.ACCEPT) {
			//	...
			//}
			//file_chooser.destroy ();
		}

		public void setText (string message) {
			this.bottomPanelLabel.text =  message;
		}
	}
}
